package com.itheima.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.attendance.mapper.AttendanceConfigMapper;
import com.itheima.attendance.pojo.AttendanceConfig;
import com.itheima.attendance.pojo.LeaveConfig;
import com.itheima.attendance.service.ConfigService;
import com.itheima.common.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/12/10 21:41
 */
@Service
@Transactional
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private AttendanceConfigMapper attendanceConfigMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 根据部门id和企业id查询考勤设置
     */
    @Override
    public AttendanceConfig findAttenByDeptementIdAndCompanyId(String depetmentId, String companyId) {
        QueryWrapper<AttendanceConfig> queryWrapper=new QueryWrapper<AttendanceConfig>();
        queryWrapper.and(i->i.eq("department_id",depetmentId).eq("company_id",companyId));
        AttendanceConfig attendanceConfig = attendanceConfigMapper.selectOne(queryWrapper);
        return attendanceConfig;
    }

    /**
     * 设置企业部门的的考勤
     */
    @Override
    public Integer setAttenByCompanyIdAndDeptementId(AttendanceConfig attendanceConfig) {
        //根据companyId和departmentId查询是否存在
        AttendanceConfig vo = this.findAttenByDeptementIdAndCompanyId(attendanceConfig.getDepartmentId(), attendanceConfig.getCompanyId());
        if(vo==null){
            //不存在，新增
            attendanceConfig.setId(String.valueOf(idWorker.nextId()));
            return attendanceConfigMapper.insert(attendanceConfig);
        }
        attendanceConfig.setId(vo.getId());
        return attendanceConfigMapper.updateById(attendanceConfig);
    }

}
